//创建界面的js
var optionNum = 2;

var userOptionNum = 1;

var $cOption;

var $cMsg;

function createSubInit() {
	var id = $('#id').val();
	//初始化起息日为母份额的创建日
	var establishStr = $('#establishStr').val();
	$('#interestdate').val(establishStr); 
	
	getContent("/company/findAll", {}, true, function(data) {
		if (data != null) {
			data.forEach(function(d){ 
				//评级机构
				if(d.type==4){
					$('#ratingId').append('<option value="'+d.id+'">'+d.name+'</option>');
				}
			});
		}
	});		
	
	if(id){
		getContent("/subProject/find", 'id=' + id, true, function(data) {
			if (data != null) {
				$('#name').val(data.name);
				$('#projectId').val(data.projectId);
				$('#shortName').val(data.shortName);
				$('#code').val(data.code);
				$('#collect').val(data.collect);
				$('#rate').val(data.rate);
				$('#paynumber').val(data.paynumber);
				$('#credit').val(data.credit);
				if(data.interestdate!=null){
					$('#interestdate').val((new Date(data.interestdate.time)).Format("MM/dd/yyyy"));
				}
				if(data.expirydate!=null){
					$('#expirydate').val((new Date(data.expirydate.time)).Format("MM/dd/yyyy"));
				}
				$('#interesttype').select2('val',data.interesttype);
				$('#paytype').select2('val',data.paytype);
				$('#ratingId').select2('val',data.ratingId);	
				
				if(data.ratingDate!=null){
					$('#ratingDate').val((new Date(data.ratingDate.time)).Format("MM/dd/yyyy"));
				}
				$('#isTrade').select2('val',data.isTrade);	
				$('#duration').val(data.duration);
				$('#payDay').val(data.payDay);
				$('#payMonth').val(data.payMonth);
			}
		});
	}
}


function modiSubProject(id) {
	var projectId = $('#projectId').val();
	var param = {};
	param.id = id;
	param.projectId = projectId;
	showContent('/view/subproject-create.html',param,createSubInit);
}

function createSubProject(projectId,establishStr) {
	var param = {};
	param.projectId = projectId;
	param.establishStr = establishStr;
	showContent('/view/subproject-create.html',param,createSubInit);
}


function saveSubProject() {

	var data = {};
	data.id = $('#id').val();
	data.projectId = $('#projectId').val();
	data.name = $('#name').val();
	data.shortName = $('#shortName').val();
	data.code = $('#code').val();
	data.collect = $('#collect').val();
	data.rate = $('#rate').val();
	data.interesttype = $('#interesttype').val();
	data.paytype = $('#paytype').val();
	data.paynumber = $('#paynumber').val();
	data.credit = $('#credit').val();
	data.interestdate = $('#interestdate').val();
	data.expirydate = $('#expirydate').val();
	data.ratingId = $('#ratingId').val();
	
	data.isTrade = $('#isTrade').val();
	data.ratingDate = $('#ratingDate').val();
	data.duration = $('#duration').val();
	data.payDay = $('#payDay').val();
	data.payMonth = $('#payMonth').val();
	var jdata = JSON.stringify(data);
	postContent("/subProject/save", jdata, true, function(data) {
		if (data != null) {
			alert('保存成功!');
		}
	});
}